package com.bright.multiphoto;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.project.weimi.R;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class AlbumActivity extends AlbumBaseActivity {
	private ArrayList<String> imageUrls;

	private ImageAdapter imageAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		initData();
		initGallery();
	}
	
	private void initData() {
		this.imageUrls = new ArrayList<String>();
		final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
		final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
		Cursor imagecursor = this.getApplicationContext().getContentResolver()
				.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy + " DESC");
		for (int i = 0; i < imagecursor.getCount(); i++) {
			imagecursor.moveToPosition(i);
			int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
			imageUrls.add("file://" + imagecursor.getString(dataColumnIndex));
			Log.i("imageUrl", imageUrls.get(i));
		}
	}

	private void initGallery() {

		imageAdapter = new ImageAdapter(this, imageUrls);
		GridView gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(imageAdapter);
	}

	@Override
	protected void onStop() {
		imageLoader.stop();
		super.onStop();
	}

	public void btnChoosePhotosClick(View v) {
		ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
//		Toast.makeText(AlbumActivity.this,
//				"Total photos selected: " + selectedItems.size() + "\nSelected Items: " + selectedItems.toString(),
//				Toast.LENGTH_SHORT).show();

		int count = selectedItems.size();
		for (int i = 0; i < count; i++) {
			String baseString = Base64Util.compress(selectedItems.get(i));
			Log.e(AlbumActivity.class.getSimpleName(), baseString.length() + "");
		}

		Intent intent = getIntent();
		intent.putStringArrayListExtra("list", selectedItems);
		setResult(0, intent);
		finish();
		
		// setViewPager(selectedItems);

	}

	private void setViewPager(ArrayList<String> selectedItems) {
		Intent intent = new Intent(this, ViewPagerPhotoActivity.class);
		Bundle bundle = new Bundle();
		bundle.putStringArrayList("selectedItems", selectedItems);
		intent.putExtra("bundle", bundle);
		startActivity(intent);
	}

	/**
	 * Description GridView Adapter
	 */
	public class ImageAdapter extends BaseAdapter {
		ArrayList<String> mList;
		LayoutInflater mInflater;
		Context mContext;
		SparseBooleanArray mSparseBooleanArray;

		public ImageAdapter(Context context, ArrayList<String> imageList) {
			// TODO Auto-generated constructor stub
			mContext = context;
			mInflater = LayoutInflater.from(mContext);
			mSparseBooleanArray = new SparseBooleanArray();
			mList = new ArrayList<String>();
			this.mList = imageList;
		}

		public ArrayList<String> getCheckedItems() {
			ArrayList<String> mTempArry = new ArrayList<String>();
			for (int i = 0; i < mList.size(); i++) {
				if (mSparseBooleanArray.get(i)) {
					mTempArry.add(mList.get(i));
				}
			}
			return mTempArry;
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
			}
			CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
			final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
			imageLoader.displayImage(imageUrls.get(position), imageView, options, new ImageLoadingListener() {

				@Override
				public void onLoadingStarted(String arg0, View arg1) {
					// TODO Auto-generated method stub
				}

				@Override
				public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
					// TODO Auto-generated method stub
					Animation anim = AnimationUtils.loadAnimation(AlbumActivity.this, android.R.anim.fade_in);// R.anim.fade_in
					imageView.setAnimation(anim);
					anim.start();
				}

				@Override
				public void onLoadingCancelled(String arg0, View arg1) {
					// TODO Auto-generated method stub
				}

				@Override
				public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
					// TODO Auto-generated method stub
				}
			});
			mCheckBox.setTag(position);
			mCheckBox.setChecked(mSparseBooleanArray.get(position));
			mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
			return convertView;
		}

		OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
			}
		};
	}
}
